const state = {
	count: 0,
	list: [1, 5, 8, 10, 30, 50]
};

const getters = {
	count: state => state.count,
	filteredList: state => state.list.filter(item => item < 10),
	listCount: (state, getters) => {
		return getters.filteredList.length;
	}
};

const mutations = {
	increment(state, n = 1) {
		console.log(n);
		state.count += n;
	},
	decrease(state, n = 1) {
		console.log(n);
		state.count -= n;
	}
};

/**
 * 因为可以直接在组件 commit mutation， 没必要通过action中转一次。但是加了异步就不一样了,需要用actions
 * mutations、 actions看起来很相似，可能会觉得不知道该用哪个
 * 但是Vuex很像是一种与开发 者的约定，所以涉及改变数据的，就使用mutations，存在业务逻辑的，就用actions
 */
const actions = {
	increment(context) {
		context.commit('increment');
	},
	asyncIncrement(context) {
		return new Promise(resolve => {
			setTimeout(() => {
				context.commit('increment');
				resolve();
			}, 1000)
		});
	}
};

export default {
	state,
	getters,
	mutations,
	actions
}

